package scalalab;

import java.io.File;
import java.net.URL;

public class JavaUtilities {

      static URL  jarPathOfClass(String className) {
        try {
            return Class.forName(className).getProtectionDomain().getCodeSource().getLocation();
        } catch (ClassNotFoundException ex) {
           System.out.println("error in jarPathOfClass"+className+")");
           ex.printStackTrace();
           return null;
        }

      }
     
      static public void detectPaths() {
          boolean hostIsUnix = true;
          if (File.pathSeparatorChar==';')
              hostIsUnix = false;  // Windows host
          
          if (hostIsUnix) {   
       JavaGlobals.jarFilePath = jarPathOfClass("scalaSci.DoubleDoubleArr").toString().replace("file:/", "/");
       JavaGlobals.compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "/");
       JavaGlobals.libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","/");
       JavaGlobals.swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "/");
       JavaGlobals.libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "/");
       JavaGlobals.VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "/");
                    }
         else {
       JavaGlobals.jarFilePath = jarPathOfClass("scalaSci.DoubleDoubleArr").toString().replace("file:/", "");
       JavaGlobals.compFile = jarPathOfClass("scala.tools.nsc.ScriptRunner").toString().replace("file:/", "");
       JavaGlobals.libFile =  jarPathOfClass("scala.ScalaObject").toString().replace("file:/","");
       JavaGlobals.swingFile = jarPathOfClass("scala.swing.Swing").toString().replace("file:/", "");
       JavaGlobals.libsFile = jarPathOfClass("JSci.maths.wavelet.Cascades").toString().replace("file:/", "");
      JavaGlobals.VISADFile = jarPathOfClass("visad.Action").toString().replace("file:/", "");
                  }   
          
          System.out.println("jarFile ="+JavaGlobals.jarFilePath);
          System.out.println("compFile = "+JavaGlobals.compFile);
          System.out.println("libFile = "+JavaGlobals.libFile);
          System.out.println("swingFile = "+JavaGlobals.swingFile);
          System.out.println("libsFile = "+JavaGlobals.libsFile);
          System.out.println("VISADFile = "+JavaGlobals.VISADFile);
      }
     
}
